//
//  UIColor+Extension.swift
//  
//
//  Created by Hoa on 2020/12/30.
//

import Foundation
import UIKit

public extension UIColor {
    
    class func hexColor(_ hex: UInt32, alpha: CGFloat = 1.0) -> UIColor {
        let r = CGFloat((hex & 0xff0000) >> 16) / 255.0
        let g = CGFloat((hex &   0xff00) >>  8) / 255.0
        let b = CGFloat((hex &     0xff)) / 255.0
        return UIColor(red:r, green:g, blue: b, alpha: alpha)
    }
    
    func asImage(_ size: CGSize) -> UIImage? {
        
        var resultImage: UIImage? = nil
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale)
        
        guard let context = UIGraphicsGetCurrentContext() else {
            
            return resultImage
        }
        
        context.setFillColor(self.cgColor)
        context.fill(rect)
        resultImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return resultImage
    }
    
    func fillBorderImage(_ image: UIImage) -> UIImage? {
        
        let size = image.size
        let scale: CGFloat = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        let path = UIBezierPath.init(roundedRect: CGRect.init(x: 0, y: 0, width: size.width, height: size.height), cornerRadius: size.height / 2.0)
        path.lineWidth = 1
        self.set()
        path.addClip()
        path.fill()
        
        let clicPath = UIBezierPath.init(roundedRect: CGRect.init(x: 1, y: 1, width: size.width - 2 , height: size.height - 2), cornerRadius: (size.height - 2) / 2.0)
        clicPath.addClip()
        
        image.draw(in: CGRect.init(x: 1, y: 1, width: size.width - 2, height: size.height - 2))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return newImage
    }
    
    /// 判断颜色是深色还是浅色
    var isDark: Bool {
        
        get {
            var red: CGFloat = 0
            var green: CGFloat = 0
            var blue: CGFloat = 0
            var alpha: CGFloat = 0
            self.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
            
            let yValue = 0.299 * red + 0.587 * green + 0.114 * blue
            
            if yValue > 0.75 {
                return false
            }
            else {
                return true
            }
        }
    }
    
    convenience init(red: Int, green: Int, blue: Int, alpha: CGFloat = 1.0) {
        assert(red >= 0 && red <= 255, "Invalid red component")
        assert(green >= 0 && green <= 255, "Invalid green component")
        assert(blue >= 0 && blue <= 255, "Invalid blue component")

        self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: alpha)
    }

    convenience init(rgb: Int, alpha: CGFloat = 1.0) {
        self.init(
            red: (rgb >> 16) & 0xFF,
            green: (rgb >> 8) & 0xFF,
            blue: rgb & 0xFF,
            alpha: alpha
        )
    }
}
